package com.giveu.signature.common.document.pdf.itext;

import com.itextpdf.awt.geom.Rectangle2D.Float;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**********************************************************************
 * <pre>
 * FILE : PositionRenderListener.java
 * CLASS : PositionRenderListener
 *
 * AUTHOR : caoxu-yiyang@qq.com
 *
 * FUNCTION : TODO
 *
 *
 *======================================================================
 * CHANGE HISTORY LOG
 *----------------------------------------------------------------------
 * MOD. NO.|   DATE   |   NAME  | REASON  | CHANGE REQ.
 *----------------------------------------------------------------------
 * 		    |2016年11月9日|caoxu-yiyang@qq.com| Created |
 * DESCRIPTION:
 * </pre>
 ***********************************************************************/

/**
 * pdf渲染监听,当找到渲染的文本时，得到文本的坐标x,y,w,h
 * @user : caoxu-yiyang@qq.com
 * @date : 2016年11月9日
 */
public class PositionRenderListener implements RenderListener{
	
	private List<String> findText;
	private float defaultH;		///出现无法取到值的情况，默认为12
	private float fixHeight;	//可能出现无法完全覆盖的情况，提供修正的参数，默认为2
	private int pageNo;
	
//	public PositionRenderListener(List<String> findText, float defaultH,float fixHeight, int pageNo) {
//		this.findText = findText;
//		this.defaultH = defaultH;
//		this.fixHeight = fixHeight;
//		this.pageNo = pageNo;
//	}

	public PositionRenderListener(List<String> findText, int pageNo) {
		this.findText = findText;
		this.defaultH = 13;
		this.fixHeight = 3f;
		this.pageNo = pageNo;
	}
	
	@Override
	public void beginTextBlock() {
		
	}

	@Override
	public void endTextBlock() {
		
	}

	@Override
	public void renderImage(ImageRenderInfo imageInfo) {
	}

	private Map<String, List<ReplaceRegion>> result = new HashMap<>();
	
	private StringBuilder sb = new StringBuilder();
	
	private TextRenderInfo firstRenderInfo;
	
	private float totalWidth;
	private float startX;
	
	
	@Override
	public void renderText(TextRenderInfo textInfo) {
		List<TextRenderInfo> characterRenderInfos = textInfo.getCharacterRenderInfos();
		for (TextRenderInfo textRenderInfo : characterRenderInfos) {
			renderAChar(textRenderInfo);
		}
		
	}
	
	/**
	 * @param textInfo
	 */
	private void renderAChar(TextRenderInfo textInfo) {
		String text = textInfo.getText();
		String paramText = null;
		if (text.equals("{")) {
			totalWidth = 0.0f;
			firstRenderInfo = textInfo;
			startX = textInfo.getBaseline().getBoundingRectange().x;
			sb.append(text);
		} 
		else if (text.equals("}")) {
			sb.append(text);
			paramText = sb.toString();
			totalWidth = textInfo.getBaseline().getBoundingRectange().x + textInfo.getBaseline().getBoundingRectange().width - startX;
			sb = new StringBuilder();
		}
		else if (sb.length() != 0) {
			sb.append(text);
		}
		if (null != paramText && firstRenderInfo != null) {
			for (String keyWord : findText) {
				if (paramText.substring(1, paramText.length() - 1).trim().equals(keyWord)){
					Float bound = firstRenderInfo.getBaseline().getBoundingRectange();
					ReplaceRegion region = new ReplaceRegion(keyWord);
					region.setH(bound.height == 0 ? defaultH : bound.height);
					region.setW(totalWidth/*bound.width*/);
					region.setX(bound.x);
					region.setY(bound.y-this.fixHeight);
					region.setPageNo(pageNo);
					List<ReplaceRegion> regionList = result.get(keyWord);
					if(regionList == null){
						regionList= new ArrayList<>();
						result.put(keyWord, regionList);
					}
					regionList.add(region);
				}
			}
			firstRenderInfo = null;
			paramText = null;
		}
	}

	public Map<String, List<ReplaceRegion>> getResult() {
		return this.result;
	}
}
